﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
//using System.Collections;
using System.Diagnostics;
using Microsoft.MediaCenter.UI;
using System.IO;
//using System.Drawing;

using Google.GData.Photos;
using Google.GData.Extensions.MediaRss;

#pragma warning disable 3001, 3002, 3003
namespace McePicasa
{
    public class PicasaAlbum : PicasaObject, IDisposable
    {
        public PicasaAlbum(PicasaEntry entry, PicasaUser owner)
        {
            myOwner = owner;
            MyUrl = entry.FeedUri;
            this.published = entry.Published;
            MediaThumbnail thumb = entry.Media.Thumbnails[0];
            this.albumAccessor = new AlbumAccessor(entry);
            MyId = albumAccessor.Id;
            MyTitle = albumAccessor.AlbumTitle;
            this.summary = albumAccessor.AlbumSummary;
            this.MyThumbnailUrl = thumb.Attributes["url"] as string;
            this.noOfPhotos = int.Parse(entry.getPhotoExtensionValue(GPhotoNameTable.NumPhotos), CultureInfo.CurrentCulture);
            photos = new List<PicasaPhoto>();
        }

        public IList<PicasaPhoto> Photos
        {
            get { return photos; }
        }

        public IList<PicasaPhoto> LoadPhotos()
        {
            PicasaFeed feed = PicasaAPI.GetAlbumPhotoFeed(MyUrl);
            if (feed == null) return null;

            foreach (PicasaEntry entry in feed.Entries)
            {
                PicasaPhoto photo = new PicasaPhoto(this, new PhotoAccessor(entry), entry.Title.Text,
                    (entry.Media.Content.Attributes["url"] as string),
                    PicasaAPI.FindLargestThumbnail(entry.Media.Thumbnails));
                photos.Add(photo);
            }
            photosLoaded = true;
            return photos;
        }

        /*
                /// <summary>
                /// Helper method to load an image from our data directory.
                /// </summary>

                private void LoadCover(string ThumbUrl)
                {
                  try
                  {
                    if (cover == null) cover = new Microsoft.MediaCenter.UI.Image(ThumbUrl);
                  }
                  catch (Exception e)
                  {
                    Log.LogIt("Error loading image: " + ThumbUrl + "\n" + e.Message);
                    throw;
                  }
                }
        */

        public int NoOfPhotos
        {
            get { return noOfPhotos; }
            set { noOfPhotos = value; }
        }

        //struct Resolution
        //{
        //    public int height;
        //    public int width;
        //}

        public PicasaPhoto GetPhoto(int index)
        {
            return photos[index];
        }

        public PicasaPhoto GetPrevPhoto(PicasaPhoto photo)
        {
            if (photos == null) return null;
            if (photos.Count < 2) return photo;
            int i;
            for (i = 0; i < photos.Count; i++)
                if (photos[i] == photo) break;
            if (i == 0) return photos[photos.Count - 1];
            return photos[i - 1];
        }

        public PicasaPhoto GetNextPhoto(PicasaPhoto photo)
        {
            if (photos == null) return null;
            if (photos.Count < 2) return photo;
            int i;
            for (i = 0; i < photos.Count; i++)
                if (photos[i] == photo) break;
            if (i < photos.Count - 1) return photos[i + 1];
            return photos[0];
        }

        public string Summary
        {
            get { return summary; }
            set { summary = value; }
        }

        public DateTime Published
        {
            get { return published; }
            set { published = value; }
        }
        public bool PhotosLoaded
        {
            get { return photosLoaded; }
            set { photosLoaded = value; }
        }

        public AlbumAccessor AlbumAccessor
        {
            get { return albumAccessor; }
            set { albumAccessor = value; }
        }

        public Image LoadImage()
        {
            string ext;
            string filename = Application.Current.AppDataPath() + @"\";

            try
            {
                filename += myOwner.Id2;
                if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);

                filename += @"\";
                filename += albumAccessor.Id;
                if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);

                ext = (MyThumbnailUrl != null) && (MyThumbnailUrl.Length > 3) ?
                    MyThumbnailUrl.Substring(MyThumbnailUrl.Length - 3) : "jpg";

                filename += @"\";
                filename += (MyId + "." + ext);
                if (File.Exists(filename))
                {
                    image = new Image("file://" + filename);
                    return image;
                }
            }
            catch (Exception e)
            {
                Global.MsgBox("LoadImage: filename = " + filename + " - " + e.Message, 20);
                throw;
            }

            try
            {
                if (PicasaAPI.SaveImage(MyThumbnailUrl, filename))
                    image = new Image("file://" + filename);
                else
                    image = null;
                return image;
            }
            catch (PicasaException e)
            {
                Log.LogIt("LoadImage: filename = " + filename + " " + e.Message);
                image = null;
                return image;
            }
        }

        public PicasaUser Owner
        {
            get { return myOwner; }
            set { myOwner = value; }
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (image != null) image.Dispose();
                //if (cover != null) cover.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private IList<PicasaPhoto> photos;
        //private Microsoft.MediaCenter.UI.Image cover;
        private int noOfPhotos;
        private string summary;
        private DateTime published;
        private bool photosLoaded;
        private AlbumAccessor albumAccessor;
        private PicasaUser myOwner;
        private Image image;

        //private string geo;
        //private string comments;
        //private float size;
        //private Resolution res;
    }
}
